package com.mz.mattress.api.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.Random;

public class DateUtil {

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static final String MIN = " 00:00:00";
    public static final String MAX = " 23:59:59";

    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS);

    public static final DateTimeFormatter DAY = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static final DateTimeFormatter YYYYMMDD = DateTimeFormatter.ofPattern("yyyyMMdd");

    public static final DateTimeFormatter YYYYMMDD_HHmmss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static final DateTimeFormatter YYYY_MM = DateTimeFormatter.ofPattern("yyyy-MM");

    public static final DateTimeFormatter yyMMdd = DateTimeFormatter.ofPattern("yyMMdd");

    public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");

    public static final SimpleDateFormat SDF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final Random random = new Random();

    public static final Date toDate(String dateStr) {

        try {
            return SDF.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static final LocalDateTime parseLocalDateTime(String str) {
        return LocalDateTime.parse(str, YYYYMMDD_HHmmss);
    }

    public static Date toDateFromLocalDate(LocalDate localDate) {
        LocalDateTime localDateTime = localDate.atStartOfDay(); // 转换为当天的开始时间
        ZoneId defaultZoneId = ZoneId.systemDefault(); // 获取系统默认时区
        Instant instant = localDateTime.atZone(defaultZoneId).toInstant(); // 转换为Instant
        return Date.from(instant);
    }

    public static final Date toDateTime(String dateStr) {

        try {
            return SDF2.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    public static final String toDateTimeStr(Date date) {

        try {
            return SDF2.format(date);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

//    public static final String getNowDateTime(Date date) {
//        //线程安全
//        date.toString()
//        return YYYYMMDD_HHmmss.parse(date);
//    }

    public static final String getNowDateTime() {
        //线程安全
        return YYYYMMDD_HHmmss.format(LocalDateTime.now());
    }

    public static final String plusMin(Integer min) {
        //线程安全
        return YYYYMMDD_HHmmss.format(LocalDateTime.now().plusMinutes(min));
    }

    public static final String getNowDay() {
        //线程安全
        return DAY.format(LocalDateTime.now());
    }

    public static final String getNowDayPlus(int days) {
        //线程安全
        return DAY.format(LocalDateTime.now().plusDays(days));
    }

    public static final String getNowDayPlusDaysMax(int days) {
        //线程安全
        return DAY.format(LocalDateTime.now().plusDays(days)) + MAX;
    }

    public static final String getNowDayMax() {
        //线程安全
        return DAY.format(LocalDateTime.now()) + MAX;
    }

    public static final String getDateTimeStr(LocalDateTime date) {
        //线程安全
        return YYYYMMDD_HHmmss.format(date);
    }

    public static final String getDateStr(TemporalAccessor date) {
        //线程安全
//        YYYYMMDD_HHmmss.p
        return YYYYMMDD_HHmmss.format(date);
    }

    /**
     * 时间戳转日期
     * @param datetime
     * @return
     */
    public static final String datetime2Date(String datetime, DateTimeFormatter formatter) {
        LocalDateTime localDateTime = LocalDateTime.parse(datetime, YYYYMMDD_HHmmss);
        return formatter.format(localDateTime);
    }

    public static final long getToNowSecond(String datetime) {
        LocalDateTime time = LocalDateTime.parse(datetime, YYYYMMDD_HHmmss);
        Duration duration = Duration.between(LocalDateTime.now(), time);
        return duration.getSeconds();
    }

    public static final String nextUseTime(String datetime) {
        LocalDateTime time = LocalDateTime.parse(datetime, YYYYMMDD_HHmmss);
        int minute = 1 + random.nextInt(5);
        time = time.plusMinutes(minute).plusSeconds(random.nextInt(47));
        return YYYYMMDD_HHmmss.format(time);
    }

    public static final String nextEndTime(String datetime) {
        LocalDateTime time = LocalDateTime.parse(datetime, YYYYMMDD_HHmmss);
        int minute = 30 + random.nextInt(30);
        time = time.plusMinutes(minute).plusSeconds(random.nextInt(47));
        return YYYYMMDD_HHmmss.format(time);
    }

    public static final String getNowDate() {
        //线程安全
        return YYYYMMDD.format(LocalDateTime.now());
    }

    public static final String getDatePlus(int days) {
        //线程安全
        return YYYYMMDD.format(LocalDateTime.now().plusDays(days));
    }

    public static final String getNowMonth() {
        //线程安全
        return YYYY_MM.format(LocalDateTime.now());
    }

    public static final String getYYMMdd(int days) {
        //线程安全
        return yyMMdd.format(LocalDateTime.now().plusDays(days));
    }


    public static void main(String[] args) {

        Date fromLocalDate = toDateFromLocalDate(LocalDate.now());
        String dateTimeStr = DateUtil.toDateTimeStr(fromLocalDate);
        System.out.println(dateTimeStr);

        String ss = DateUtil.plusMin(3);
        System.out.println(ss);

//        long toNowSecond = getToNowSecond("2025-03-12 21:18:23");
//        System.out.println(toNowSecond);


        String dd = nextEndTime("2025-03-12 21:18:23");
        System.out.println(dd);

        String dd2 = nextUseTime("2025-03-12 21:18:23");
        System.out.println(dd2);

    }


}
